package net.skyaims.aiolia.chat.litp;

import com.skyaims.biz.factory.MessageFactory;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.MessageEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LitpDispatchEventHandler implements ChannelUpstreamHandler {

    private static final Logger log = LoggerFactory.getLogger(LitpDispatchEventHandler.class);

    @Override
    public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
        if (e instanceof MessageEvent) {
            messageProcess(ctx, (MessageEvent) e);
        } else {
            log.info("ignore channel event {}", e.getClass().getName());
        }
    }

    public void messageProcess(ChannelHandlerContext ctx, MessageEvent e) {
        try {
            MessageFactory.receive(ctx, (ChannelBuffer) e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

}
